package com.itheima.cart.service.impl;

import com.itheima.cart.dto.ShoppingCartDTO;
import com.itheima.cart.feign.DishClient;
import com.itheima.cart.feign.SetmealClient;
import com.itheima.cart.mapper.ShoppingCartMapper;
import com.itheima.cart.pojo.ShoppingCart;
import com.itheima.cart.service.ShoppingCartService;
import com.itheima.context.BaseContext;
import com.itheima.goods.dto.SetmealDTO;
import com.itheima.goods.pojo.Dish;
import com.itheima.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

//    @Autowired
//    private DishMapper dishMapper;
    @Autowired
    private DishClient dishClient;

//    @Autowired
//    private SetmealService setmealService;
    @Autowired
    private SetmealClient setmealClient;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();

        //先去购物车表查一下是否存在
        List<ShoppingCart> carts = shoppingCartMapper.getByDishIdOrSetmealIdWithUserId(shoppingCart);
        if (carts != null && carts.size() > 0) {
            //修改
            ShoppingCart cart = carts.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
            return;
        }

        //插入
        if (shoppingCartDTO.getDishId() != null) {

//            Dish dish = dishMapper.findByDishId(shoppingCartDTO.getDishId());
            Result<Dish> result = dishClient.findByDishId(shoppingCartDTO.getDishId());
            Dish dish = result.getData();
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());


        }
        if (shoppingCartDTO.getSetmealId() != null) {
//            SetmealDTO setmealDTO = setmealService.findById(shoppingCartDTO.getSetmealId());
            Result<SetmealDTO> result = setmealClient.find(shoppingCartDTO.getSetmealId());
            SetmealDTO setmealDTO = result.getData();
            shoppingCart.setName(setmealDTO.getName());
            shoppingCart.setImage(setmealDTO.getImage());
            shoppingCart.setAmount(setmealDTO.getPrice());

        }
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public List<ShoppingCart> show() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.getByDishIdOrSetmealIdWithUserId(shoppingCart);
    }

    @Override
    public void clean() {

        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void reduce(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .dishId(shoppingCartDTO.getDishId())
                .setmealId(shoppingCartDTO.getSetmealId())
                .dishFlavor(shoppingCartDTO.getDishFlavor())
                .build();

        //先去购物车表查一下是否存在
        List<ShoppingCart> carts = shoppingCartMapper.getByDishIdOrSetmealIdWithUserId(shoppingCart);
        if (carts != null && carts.size() > 0) {
            //修改
            ShoppingCart cart = carts.get(0);
            cart.setNumber(cart.getNumber() - 1);
            if (cart.getNumber()==0){
                //数量为一就删除
                shoppingCartMapper.deleteByDishId(cart);
            }
            shoppingCartMapper.update(cart);

        }
    }

    @Override
    public List<ShoppingCart> getByDishIdOrSetmealIdWithUserId(ShoppingCart shoppingCart) {

        List<ShoppingCart> byDishIdOrSetmealIdWithUserId = shoppingCartMapper.getByDishIdOrSetmealIdWithUserId(shoppingCart);
        return byDishIdOrSetmealIdWithUserId;
    }

    @Override
    public void addshoppingCart(ShoppingCart shoppingCart) {
        shoppingCartMapper.add(shoppingCart);
    }

    @Override
    public void cleanmCart(Long userId) {
        shoppingCartMapper.clean(userId);
    }


}